package junk;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.opera.OperaDriver;
import org.openqa.selenium.opera.OperaOptions;
import org.testng.annotations.Test;

import java.io.File;
import java.io.IOException;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;

import static org.testng.Assert.assertEquals;

public class webdriver_do1 {

    public WebDriver wd;


    @Test
            /*    @Parameters("url_hub")*/
    public void init_webdriver() throws IOException {


     /*   String url_hub = "http://192.168.1.7:4444/wd/hub/";


        DesiredCapabilities browser = DesiredCapabilities.chrome();*/


        String driverPath = new File("./").getCanonicalPath() + "/src/test/java/driver/geckodriver.exe";
        String driverName = "webdriver.gecko.driver";

/*
        System.setProperty(driverName, driverPath);

        FirefoxOptions firefoxOptions = new FirefoxOptions ();

        firefoxOptions.addArguments("--headless");

      //  firefoxOptions.addArguments("--window-size=1920,1080");

            wd=new FirefoxDriver(firefoxOptions);
*/
       wd=new FirefoxDriver();





        wd.manage().window().maximize();
       wd.get("http://www.baidu.com");
     WebElement baidu= wd.findElement(By.id("kw"));
      baidu.sendKeys("selenium3");
     wd.findElement(By.id("su")).click();
        System.out.println("-----webdriver初始化完毕！！！----\n");

        /*截图*/
      takeScreenshot(wd);

        wd.quit();

   /*     try {
            Thread.sleep(3600);

        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        */
      //  test_wd();

    }


    public void PlaySpeed(){
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }



    public void test_wd() {
        try {
            Thread.sleep(3600);
            wd.close();
            wd.quit();
            System.out.println("----close browser!!!-----");

        } catch (InterruptedException e) {
            e.printStackTrace();
        }
         System.out.println("wd ---ok");
    }

    /**
     * 是否存在某个节点
     *
     * @param by
     * @param wd
     * @return
     */
    public boolean isPresentEle(By by, WebDriver wd) {
        try {
            wd.findElement(by);
            return true;
        } catch (Exception e) {
            return false;
        }
    }


    /**
     * 获取当前时间，并转换为字符串
     * @return
     */
    private static String Date2String(){
        Format format = new SimpleDateFormat("yyyyMMddHHmmss");
        return format.format(new Date());
    }

    /**
     * 自动截屏
     * @param wd
     */
    public  void takeScreenshot(WebDriver wd){
        try {
            //获取截图file
            File srcFile= ((TakesScreenshot)wd).getScreenshotAs(OutputType.FILE);


            //新文件
            File targeFile = new File("pic/" + Date2String()+".png");

            //将图片移动到指定位置
            FileUtils.copyFile(srcFile, targeFile);
        }  catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取断言失败的原因、信息和截屏
     * @param one
     * @param two
     * @param wd
     * @throws IOException
     * @throws InterruptedException
     */
    public  void assertEqu(String one,String two,WebDriver wd) throws InterruptedException, IOException{
        try{
            Thread.sleep(3000);
            takeScreenshot(wd);
            assertEquals(one, two);

        }catch(Exception e){
            System.out.println(e.getCause());
            System.out.println(e.getMessage());
        }
    }




}
		
	
	

	
